package thirdPartyUtils;

import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.*;
import io.reactivex.rxjava3.disposables.Disposable;
import io.reactivex.rxjava3.schedulers.Schedulers;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

@Slf4j
public class RxJavaTest {

    @Test
    public void test1() {
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> emitter) throws Exception {
                // ObservableEmitter 事件发射器，向观察者发送事件
                emitter.onNext(1);
                emitter.onNext(2);
                emitter.onNext(3);
                emitter.onComplete();
                System.out.println("subscribe: "+ Thread.currentThread().getName());
            }
        }).observeOn(Schedulers.io())
                .subscribe(new Observer<Integer>() {
            @Override
            public void onSubscribe(Disposable d) {
                System.out.println("onSubscribe: "+d.isDisposed());
                System.out.println("onSubscribe: "+ Thread.currentThread().getName());
            }

            @Override
            public void onNext(Integer value) {
                System.out.println("对Next事件作出响应" + value);
                System.out.println("onNext: "+ Thread.currentThread().getName());
            }

            @Override
            public void onError(Throwable e) {
                System.out.println("对Error事件作出响应");
                System.out.println("onError: "+ Thread.currentThread().getName());
            }

            @Override
            public void onComplete() {
                System.out.println("对Complete事件作出响应");
                System.out.println("onComplete: "+ Thread.currentThread().getName());
            }
        });
    }

    @Test
    public void test2(){
        Observable.just("long", "longer", "longest")
                .doOnNext(c -> System.out.println("processing item on thread " + Thread.currentThread().getName()))
                .map(String::length)
                .subscribe(length -> System.out.println("item length " + length));
    }
}
